package com.chenwut.annotation;

import org.junit.Test;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author ChenPi
 * @since 2024/09/24 15:19
 */
public class AnnotationDemo {
    // 目标：解析注解
    @Test
    public void test1Stroling() {
        // 1.获取类对象
        Class<Demo> clazz = Demo.class;

        // 2.使用isAnnotationPresent注解判断注解对象上是否有 MyTest 注解
        if (clazz.isAnnotationPresent(MyTest1.class)) {
            // 3.获取注解对象
            MyTest1 mt = clazz.getDeclaredAnnotation(MyTest1.class);
            // 4.获取注解元素值
            String[] address = mt.address();
            int height = mt.height();
            String value = mt.value();

            System.out.println(Arrays.toString(address));
            System.out.println(height);
            System.out.println(value);
        }
    }

    @Test
    public void test2() throws NoSuchMethodException {
        // 1.获取类对象
        Class<Demo> clazz = Demo.class;

        // 2.获取方法对象
        Method method = clazz.getDeclaredMethod("go");
        if (method.isAnnotationPresent(MyTest1.class)) {
            // 3.获取注解对象
            MyTest1 mt = method.getDeclaredAnnotation(MyTest1.class);
            // 4.获取注解元素值
            String[] address = mt.address();
            int height = mt.height();
            String value = mt.value();

            System.out.println(Arrays.toString(address));
            System.out.println(height);
            System.out.println(value);
        }
    }
}
